package YNU.View.impl;

import YNU.View.StudentView;
import YNU.dao.StudentDao;
import YNU.dao.impl.StudentDaoImpl;
import YNU.po.Student;


import java.util.List;
import java.util.Scanner;

public class StudentViewImpl implements StudentView {
    private Scanner input = new Scanner(System.in);
    public void listStudentAll(){
        StudentDao dao = new StudentDaoImpl();
        List<Student> list = dao.listStudent(null,null);
        System.out.println("编号\t名称\t密码\t数学\t英语");
        for(Student b : list) {
            System.out.println(b.getSid()+"\t"+b.getName()+"\t"+b.getPassword()
                    +"\t"+b.getMath()+"\t"+b.getEnglish());
        }
    }

    public void listStudent(){
        String name = "";
        String password = "";

        String inputStr = "";
//        System.out.println("是否需要输名称关键词(y/n)：");
//        inputStr = input.next();
//        if(inputStr.equals("y")) {
           System.out.println("请输入名字：");
            name = input.next();
//        }
            System.out.println("请输入密码：");
            password = input.next();


        StudentDao dao = new StudentDaoImpl();
        List<Student> list = dao.listStudent(name,password);
        System.out.println("数学成绩\t语文成绩\t总成绩");
        for(Student b : list) {
            int sum=b.getMath()+b.getEnglish();
            System.out.println(b.getMath()+"\t\t"+b.getEnglish()+"\t\t"+sum);
        }
    }

    public void saveStudent(){
        System.out.println("请输入学生名称：");
        String name = input.next();
        StudentDao dao = new StudentDaoImpl();
        int sid = dao.saveStudent(name);
        if(sid>0) {
            System.out.println("新建学生成功！学生编号为："+sid);
        }else {
            System.out.println("新建学生失败！");
        }
    }
    public void removeStudent(){
        System.out.println("请输入学生编号：");
        int sid = input.nextInt();

        StudentDao dao = new StudentDaoImpl();
       // System.out.println("确认要删除吗(y/n)：");
      //  if(input.next().equals("y")) {
            int result = dao.removeStudent(sid);
            if(result==1) {
                System.out.println("删除学生信息成功！");
            }else {
                System.out.println("删除学生信息失败！");
            }
        //}
    }
    public Student login(){
        System.out.println("登录系统需要输入学生编号和密码");
        System.out.println("请输入学生编号：");
        int sid = input.nextInt();
        System.out.println("请输入密码：");
        String password = input.next();

        StudentDao dao = new StudentDaoImpl();
        return dao.getStudentByIdByPass(sid, password);
    }
    public void showStudent(Integer sid){
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.getStudentById(sid);
        System.out.println(student);
    }
    public void editStudent(Integer sid){

        StudentDao dao = new StudentDaoImpl();
        Student student = dao.getStudentById(sid);
        System.out.println(student);

        String inputStr = "";
        System.out.println("是否修改学生名字(y/n)：");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的学生名称：");
            student.setName(input.next());
        }

        System.out.println("是否修改学生密码(y/n)：");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的学生密码：");
            student.setPassword(input.next());
        }

        System.out.println("是否修改数学成绩(y/n)：");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的数学成绩：");
            student.setMath(input.nextInt());
        }

        System.out.println("是否修改英语成绩(y/n)：");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的英语成绩：");
            student.setEnglish(input.nextInt());
        }


        int result = dao.updateStudent(student);
        if(result>0) {
            System.out.println("\n修改学生信息成功！\n");
        }else {
            System.out.println("\n修改学生信息失败！\n");
        }
    }
    public void updateStudentByPassword(Integer sid){
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.getStudentById(sid);

        System. out.println("\n请输入旧密码：");
        String oldPass = input.next();
        System.out.println("\n请输入新密码：");
        String password = input.next();
        String beginPassword=password;
//		System.out.println("\n请再次输入新密码：");
//		String beginPassword = input.next();

        if(!student.getPassword().equals(oldPass)) {
            System.out.println("\n旧密码输入错误！");
        }else if(!password.equals(beginPassword)) {
            System.out.println("\n两次输入密码不一致！");
        }else {
            int result = dao.updateStudentByPassword(sid, password);
            if(result>0) {
                System.out.println("\n修改密码成功！");
            }else {
                System.out.println("\n修改密码失败！");
            }
        }
    }
    }


